package com.viknix.threadpool.manager.client.model;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import static com.viknix.threadpool.manager.common.constant.Constants.UNKNOWN;

/**
 * @Author: Dongqi
 * @Date: 2021/10/22 13:31
 * @Version 1.0
 * @Description: 当前示例信息
 */
@Data
@Slf4j
public class InstanceInfo {

    private String appName = UNKNOWN;

    private String hostName;

    private String instanceId;

    private String ipApplicationName;

    private volatile Long lastUpdatedTimestamp;

    private volatile InstanceStatus status = InstanceStatus.UP;

    public InstanceInfo() {
        this.lastUpdatedTimestamp = System.currentTimeMillis();
    }

    public void setLastUpdatedTimestamp() {
        this.lastUpdatedTimestamp = System.currentTimeMillis();
    }

    public InstanceStatus getStatus() {
        return status;
    }

    public enum InstanceStatus {

        UP,

        DOWN,

        STARTING,

        OUT_OF_SERVICE,

        UNKNOWN;

        public static InstanceStatus toEnum(String s) {
            if (s != null) {
                try {
                    return InstanceStatus.valueOf(s.toUpperCase());
                } catch (IllegalArgumentException e) {
                    // ignore and fall through to unknown
                    log.debug("illegal argument supplied to InstanceStatus.valueOf: {}, defaulting to {}", s, UNKNOWN);
                }
            }
            return UNKNOWN;
        }
    }

}

